package com.ghsc.ghxc.jsb.ui.live.view.video
import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.Surface
import android.view.View
import android.widget.ImageView
import android.widget.SeekBar
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import android.widget.TextView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.utils.GlideUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.ui.live.bean.VideoDataBean

class CustCoverVideo : StandardGSYVideoPlayer {
    //封面
    private var mCoverImage: ImageView? = null

    //底部视频信息
    private var videoTitle: TextView? = null
    private var videoUserName: TextView? = null

//    //关联商品信息
//    private var videoProductLayout: View? = null
//    private var closeVideoProductBtn: View? = null
//    private var videoProductImage: ImageView? = null
//    private var videoProductName: TextView? = null
//    private var videoProductBuyBtn: View? = null
//    private var videoProductPrice: TextView? = null

    //分享
    private var videoSharetBtn: View? = null

    //收藏信息
    private var videoCollectBtn: View? = null
    private var videoCollectImage: ImageView? = null
    private var videoCollectNum: TextView? = null

    //评论信息
    private var videoCommentBtn: View? = null
    private var videoCommentNum: TextView? = null

    //点赞信息
    private var videoPraiseBtn: View? = null
    private var videoPraiseImage: ImageView? = null
    private var videoPraiseNum: TextView? = null




    //用户信息
    private var videoUserHead: ImageView? = null
    private var videoAddUserBtn: ImageView? = null

    private var index = 0
    private var videoData: VideoDataBean? = null

    private var onMyClickListener: ((view: View?, position: Int) -> Unit)? = null

    fun setOnClickListener(listener: ((view: View?, position: Int) -> Unit)?) {
        onMyClickListener = listener
    }

    constructor(context: Context?, fullFlag: Boolean?) : super(context, fullFlag) {}
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}

    override fun init(context: Context) {
        super.init(context)
        initView()
        setListener()
        if (mThumbImageViewLayout != null &&
            (mCurrentState == -1 || mCurrentState == CURRENT_STATE_NORMAL || mCurrentState == CURRENT_STATE_ERROR)
        ) {
            mThumbImageViewLayout.visibility = VISIBLE
        }
        setGSYVideoProgressListener { progress, _, currentPosition, _ ->
            videoData?.currentPosition = currentPosition
            mProgressBar.progress = progress.toInt()
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.video_layout_cover
    }

    fun setCoverImageScaleType(scaleType: ImageView.ScaleType) {
        mCoverImage?.scaleType = scaleType
    }

    fun loadCoverImage(url: String?) {
        mCoverImage?.let {
            Glide.with(context.applicationContext)
                .setDefaultRequestOptions(RequestOptions().frame(1000000))
                .load(url)
                .into(it)
        }
    }

    fun loadCoverImageBy(id: Int) {
        mCoverImage?.setImageResource(id)
    }

    override fun cloneParams(from: GSYBaseVideoPlayer, to: GSYBaseVideoPlayer) {
        super.cloneParams(from, to)
        val sf = from as CustCoverVideo
        val st = to as CustCoverVideo
        st.mShowFullAnimation = sf.mShowFullAnimation
    }

    /******************* 下方两个重载方法，在播放开始前不屏蔽封面，不需要可屏蔽  */
    override fun onSurfaceUpdated(surface: Surface) {
        super.onSurfaceUpdated(surface)
        if (mThumbImageViewLayout != null && mThumbImageViewLayout.visibility == VISIBLE) {
            mThumbImageViewLayout?.postDelayed({
                mThumbImageViewLayout.visibility = INVISIBLE
            }, 50)
        }
    }

    override fun setViewShowState(view: View?, visibility: Int) {
        if (view === mThumbImageViewLayout && visibility != VISIBLE) {
            return
        }
        super.setViewShowState(view, visibility)
    }

    override fun onSurfaceAvailable(surface: Surface) {
        super.onSurfaceAvailable(surface)
        if (GSYVideoType.getRenderType() != GSYVideoType.TEXTURE) {
            if (mThumbImageViewLayout != null && mThumbImageViewLayout.visibility == VISIBLE) {
                mThumbImageViewLayout.visibility = INVISIBLE
            }
        }
    }

    /******************* 下方重载方法，在播放开始不显示底部进度和按键，不需要可屏蔽  */
    private var byStartedClick = false

    override fun onClickUiToggle(e: MotionEvent?) {
        if (mIfCurrentIsFullscreen && mLockCurScreen && mNeedLockFull) {
            setViewShowState(mLockScreen, VISIBLE)
            return
        }
        byStartedClick = true
        super.onClickUiToggle(e)
    }

    override fun changeUiToNormal() {
        super.changeUiToNormal()
        byStartedClick = false
    }

    override fun changeUiToPreparingShow() {
        super.changeUiToPreparingShow()
        setViewShowState(mBottomContainer, INVISIBLE)
        setViewShowState(mStartButton, INVISIBLE)
    }

    override fun changeUiToPlayingBufferingShow() {
        super.changeUiToPlayingBufferingShow()
        if (!byStartedClick) {
            setViewShowState(mBottomContainer, INVISIBLE)
            setViewShowState(mStartButton, INVISIBLE)
        }
    }

    override fun changeUiToPlayingShow() {
        super.changeUiToPlayingShow()
        if (!byStartedClick) {
            setViewShowState(mBottomContainer, INVISIBLE)
            setViewShowState(mStartButton, INVISIBLE)
        }
    }

    override fun startAfterPrepared() {
        super.startAfterPrepared()
        setViewShowState(mBottomContainer, INVISIBLE)
        setViewShowState(mStartButton, INVISIBLE)
        setViewShowState(mBottomProgressBar, VISIBLE)
    }

    override fun onStartTrackingTouch(seekBar: SeekBar?) {
        byStartedClick = false
//        byStartedClick = true
        super.onStartTrackingTouch(seekBar)
    }

    private fun initView() {
        mCoverImage = findViewById(R.id.thumbImage)
        //底部视频信息
        videoTitle = findViewById(R.id.tv_video_title)
        videoUserName = findViewById(R.id.tv_video_user_name)

        //关联商品信息
//        videoProductLayout = findViewById(R.id.video_product_layout)
//        closeVideoProductBtn = findViewById(R.id.btn_close_video_product)
//        videoProductImage = findViewById(R.id.iv_video_product_image)
//        videoProductName = findViewById(R.id.tv_video_product_name)
//        videoProductBuyBtn = findViewById(R.id.btn_video_product_buy)
//        videoProductPrice = findViewById(R.id.tv_video_product_price)



        videoSharetBtn= findViewById(R.id.btn_video_share)

        //收藏信息
        videoCollectBtn = findViewById(R.id.btn_video_collect)
        videoCollectImage = findViewById(R.id.iv_video_collect)
        videoCollectNum = findViewById(R.id.tv_video_collect)

        //评论信息
        videoCommentBtn = findViewById(R.id.btn_video_comment)
        videoCommentNum = findViewById(R.id.tv_video_comment)

        //点赞信息
        videoPraiseBtn = findViewById(R.id.btn_video_praise)
        videoPraiseImage = findViewById(R.id.iv_video_praise)
        videoPraiseNum = findViewById(R.id.tv_video_praise)

        //用户信息
        videoUserHead = findViewById(R.id.iv_video_user_head)
        videoAddUserBtn = findViewById(R.id.btn_add_video_user)
    }

    @SuppressLint("SetTextI18n")
    fun setVideoInfo(position: Int, dataInfo: VideoDataBean?) {
        index = position
        videoData = dataInfo

        //底部视频信息
        val content = dataInfo?.title
        if (content.isNullOrBlank()) {
            if (videoTitle?.isGone != true) {
                videoTitle?.visibility = View.GONE
            }
        } else {
            videoTitle?.text = content
            if (videoTitle?.isVisible != true) {
                videoTitle?.visibility = View.VISIBLE
            }
        }
        videoUserName?.text = "@${dataInfo?.nickname ?: "国画商城"}"

        //收藏信息
        videoCollectImage?.setImageResource(
//            if (dataInfo!!.ifcollect) {
//                R.drawable.icon_v_coll_true
//            } else {
//                R.drawable.icon_v_coll_false
//            }
                    R.drawable.icon_v_coll_false
        )
        videoCollectNum?.text = (dataInfo?.collects ?: "0").toString()

        //点赞信息
        videoPraiseImage?.setImageResource(
//            if (dataInfo!!.iflike) {
//                R.drawable.icon_v_zan_true
//            } else {
//                R.drawable.icon_v_zan_false
//            }
            R.drawable.icon_v_zan_false
        )
        videoPraiseNum?.text = (dataInfo?.likes ?: "0").toString()

        //评论信息
        videoCommentNum?.text = (dataInfo?.comments ?: "0").toString()

        //用户信息
        val userHead = dataInfo?.headimg
        if (userHead.isNullOrBlank()) {
            videoUserHead?.setImageResource(R.mipmap.ic_launcher)
        } else {
//            videoUserHead?.loadHeadImage(userHead)
            GlideUtil.ImageLoad(context,userHead,videoUserHead)
        }
        //关注按钮
//        videoAddUserBtn?.visibility = if (dataInfo?.iffollow == true
//            || dataInfo?.isMyDynamic() == true
//        ) View.GONE else View.VISIBLE

//        //关联商品信息
//        if (dataInfo?.isShowProduct() == true) {
//            val priceSpanString = SpannableString("¥${dataInfo.product?.getPriceStr() ?: "0.00"}")
//            priceSpanString.setSpan(
//                RelativeSizeSpan(1.6f),
//                1,
//                priceSpanString.length,
//                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
//            )
//            priceSpanString.setSpan(
//                StyleSpan(Typeface.BOLD),
//                1,
//                priceSpanString.length,
//                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
//            )
//
//            videoProductLayout?.visibility = View.VISIBLE
////            videoProductImage?.loadPNG(dataInfo.product?.imgUrl)
//            GlideUtil.ImageLoad(context,dataInfo.product?.imgUrl,videoProductImage)
//            videoProductName?.text = dataInfo.product?.productName
//            videoProductPrice?.text = priceSpanString
//            return
//        }
//        videoProductLayout?.visibility = View.GONE
    }

    private fun setListener() {
        videoCollectBtn?.setSingleClickListener {
            onMyClickListener?.invoke(videoCollectBtn, index)
        }
        videoPraiseBtn?.setSingleClickListener {
            onMyClickListener?.invoke(videoPraiseBtn, index)
        }
        videoCommentBtn?.setSingleClickListener {
            onMyClickListener?.invoke(videoCommentBtn, index)
        }
        videoUserHead?.setSingleClickListener {
            onMyClickListener?.invoke(videoUserHead, index)
        }
        videoAddUserBtn?.setSingleClickListener {
            onMyClickListener?.invoke(videoAddUserBtn, index)
        }
        videoSharetBtn?.setSingleClickListener {
            onMyClickListener?.invoke(videoSharetBtn, index)
        }
//        videoProductBuyBtn?.setSingleClickListener {
//            onMyClickListener?.invoke(videoProductBuyBtn, index)
//        }
//        closeVideoProductBtn?.setSingleClickListener {
//            videoProductLayout?.visibility = View.GONE
//        }
    }
}